<template>
  <div style="height: 6vh;background-color:#fff;padding: 10px 20px;">
    <span style="line-height:60px;font-size:20px;">员工个人信息管理</span>
    <el-button type="primary" style="float: right;margin-top: 10px;"
               @click="beforeAddEmpe">新建员工</el-button>
  </div>
  <el-dialog
      :title="dialogTitle"
      style="width: 1000px;padding: 40px;"
      v-model="dialogVisible"
      :before-close="handleClose">
    <el-form label-position="top" label-width="80px">
      <el-row :gutter="30" style="margin-top: 15px;">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input placeholder="请输入员工姓名" v-model="saveEmpeForm.name"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年龄">
            <el-input placeholder="请输入员工年龄" v-model="saveEmpeForm.age"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别">
            <el-select placeholder="请选择员工性别" v-model="saveEmpeForm.gender">
              <el-option v-for="item in genderOptions" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学历">
            <el-select placeholder="请选择员工学历" v-model="saveEmpeForm.degree" >
              <el-option v-for="item in degreeOptions" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      <el-col :span="12">
        <el-form-item label="生日" >
          <el-date-picker style="width:100vb;" placeholder="请输入员工生日" v-model="saveEmpeForm.birthday"/>
        </el-form-item>
      </el-col>
        <el-col :span="12">
          <el-form-item label="婚姻状况">
            <el-select placeholder="请选择员工婚姻状况" v-model="saveEmpeForm.marriage">
              <el-option v-for="item in marriageOptions" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号码">
            <el-input placeholder="请输入员工手机号码" v-model="saveEmpeForm.phone"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱">
            <el-input placeholder="请输入员工邮箱" v-model="saveEmpeForm.email"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="家庭地址">
            <el-input placeholder="请输入员工家庭地址" v-model="saveEmpeForm.address"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="现居地址">
            <el-input placeholder="请输入员工现居地址" v-model="saveEmpeForm.currentAddress"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="紧急联系人">
            <el-input placeholder="请输入紧急联系人姓名" v-model="saveEmpeForm.emergencyContact"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="紧急联系人电话">
            <el-input placeholder="请输入紧急联系人电话" v-model="saveEmpeForm.contactPhone"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveEmpe" v-if="showSave">保存</el-button>
    </template>
  </el-dialog>

  <!-- 用户搜索卡片 -->
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="员工姓名" style="width: 220px;">
        <el-input placeholder="请输入" v-model="searchEmpeForm.name" @keydown.enter="loadEmpe"/>
      </el-form-item>
      <el-form-item label="手机号码" style="width: 280px;">
        <el-input placeholder="请输入" v-model="searchEmpeForm.phone" @keydown.enter="loadEmpe"/>
      </el-form-item>
      <el-form-item label="居住地址" style="width: 350px;">
        <el-input placeholder="请输入" v-model="searchEmpeForm.currentAddress" @keydown.enter="loadEmpe"/>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadEmpe">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 用户表格卡片 -->
  <el-card style="margin: 20px;">
    <el-table :data="EmpeArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column prop="name" label="姓名" width="100" align="center"></el-table-column>
      <el-table-column prop="age" label="年龄" width="100" align="center"></el-table-column>
      <el-table-column prop="gender" label="性别" width="80" align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号码" width="150" align="center"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="200" align="center"></el-table-column>
      <el-table-column prop="currentAddress" label="居住地址" width="250" align="center"></el-table-column>
      <el-table-column prop="createTime" label="新增时间" width="150" align="center"></el-table-column>
      <el-table-column prop="emergencyContact" label="紧急联系人" width="120" align="center"></el-table-column>
      <el-table-column prop="contactPhone" label="紧急联系电话" width="150" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" :disabled="scope.row.level==40"
                     @click="showEmpe(scope.row.id)">查看</el-button>
          <el-button link type="primary" :disabled="scope.row.level==40"
                     @click="editEmpe(scope.row.id)">编辑</el-button>
          <el-button link type="primary" :disabled="scope.row.level==40"
                     @click="deleteEmpe(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";
import qs from "qs";
import index from "vuex";

onMounted(()=>{
  loadEmpe();
})

const showSave = ref(true);

const dialogVisible = ref(false);
//定义变量用来控制弹窗的标题
const dialogTitle = ref('新增员工');

const beforeAddEmpe = ()=>{
  dialogVisible.value = true;
  dialogTitle.value = '新增员工';
}

const saveEmpeForm = ref({
  name:'',
  age:'',
  gender:'',
  degree:'',
  birthday:'',
  marriage:'',
  phone:'',
  email:'',
  address:'',
  currentAddress:'',
  emergencyContact:'',
  contactPhone:'',
  createTime:'',
  updateTime:''
});



const saveEmpe = ()=>{
  let data = qs.stringify(saveEmpeForm.value);
  console.log(data)
  axios.post(BASE_URL+'/v1/employee/save',data)
      .then((response)=>{
        if(response.data.code == 2000){
          ElMessage.success('保存成功!');
          dialogVisible.value = false;
          saveEmpeForm.value = {};
          loadEmpe();
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}

const handleClose = ()=>{
  if(confirm('您确认要关闭本窗口吗?')){
    dialogVisible.value = false;
    saveEmpeForm.value = {};
    showSave.value = true;
  }
}

const searchEmpeForm = ref({name:'', phone:'',currentAddress:''});

const resetSearch = ()=>{
  searchEmpeForm.value = {};
  loadEmpe();
}

const EmpeArr = ref([]);

const loadEmpe = ()=>{
  let data = qs.stringify(searchEmpeForm.value);
  axios.get(BASE_URL+'/v1/employee/select?'+data)
      .then((response)=>{
        if(response.data.code == 2000){
          EmpeArr.value = response.data.data;
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}

const showEmpe = (id)=>{
  dialogVisible.value = true;
  dialogTitle.value = '查看员工个人信息';
  showSave.value = false;
  axios.get(BASE_URL+'/v1/employee/select?id='+id).then((response)=>{
    if(response.data.code == 2000){
      saveEmpeForm.value = response.data.data[0];
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

const editEmpe = (id)=>{
  dialogVisible.value = true;
  dialogTitle.value = '编辑员工个人信息';
  axios.get(BASE_URL+'/v1/employee/select?id='+id).then((response)=>{
    if(response.data.code == 2000){
      saveEmpeForm.value = response.data.data[0];
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

const deleteEmpe = (id)=>{
  if(confirm('您确认删除该员工吗?')){
    axios.post(BASE_URL+'/v1/employee/delete/'+id)
        .then((response)=>{
          if(response.data.code == 2000){
            ElMessage.success('删除成功!');
            loadEmpe();
          }else{
            ElMessage.error(response.data.msg);
          }
        })
  }
}

const genderOptions = ref([
  {label:'男',value:'1'},
  {label:'女',value:'0'}
]);

const degreeOptions=ref([
  {label:'小学',value:'10'},
  {label:'初中',value:'20'},
  {label:'高中',value:'30'},
  {label:'本科',value:'40'},
  {label:'研究生',value:'50'}
]);

const marriageOptions=ref([
  {label:'已婚',value:'10'},
  {label:'未婚',value:'20'},
  {label:'其它',value:'30'}
]);

// const titleFormatter=(row,column,cellValue,index)=>{
//   for (let item of levelOptions.value) {
//     if (item.value==cellValue) {
//       cellValue = item.label;
//     }
//   }
//   return cellValue;
// }

</script>


<style scoped>

</style>